#include "Stdafx.h"

#include "EngineTests.h"


 // doing and undoing moves
///////////////////////////////////////////////////////////

int inline GetTheDirection(BYTE dwSrc, BYTE dwTrg)
{
	int diff = dwTrg - dwSrc;
	if((diff % 4) == 0)
		return (abs(diff) / diff) * 4;
	else
		return (abs(diff) / diff) * 5;
}

void inline ChangeToKing(TGameBoard board, BYTE dwField, ZOBRIST_KEY& boardKey)
{

	if( IS_WHITEFIGURE( board[dwField] ) == true )	
		board[dwField] = WHITE_King;
	else 
		board[dwField] = BLACK_King;

}

void inline ChangeToMan(TGameBoard board, BYTE dwField, ZOBRIST_KEY& boardKey)
{

	if( IS_WHITEFIGURE( board[dwField] ) == true )
		board[dwField] = WHITE_MAN;
	else 
		board[dwField] = BLACK_MAN;

}

void DoMove(TGameBoard board, TSingleMove* pMove, bool bJump, ZOBRIST_KEY& boardKey)
{
	bool bInterruptMove = true;
	bool bWhite = true;

	int stepCount = 0;
	BYTE* pMoveStep = pMove->MovePositions;
	BYTE* pNextStep = pMoveStep + 1;

	// detect interrupt move
	BYTE dwFigure = board[*pMoveStep];
	if( (dwFigure == WHITE_King || dwFigure == BLACK_King) && bJump == false)
		bInterruptMove = false;
	if(dwFigure == BLACK_King || dwFigure == BLACK_MAN)
		bWhite = false;
	

	while(*pNextStep != 0)
	{
		register BYTE dwCurField = *pMoveStep;
		register BYTE dwNextField = *pNextStep;

		if(board[dwNextField] == BORDER)
		{
			assert(FALSE);
		}


		board[ dwNextField ] = board[ dwCurField ];
		board[ dwCurField ] = 0;
		


		if(bJump == true)
		{
			register BYTE dwJumpField = pMove->RemovePositions[stepCount];
			if(board[dwJumpField] == BORDER)
			{
				assert(FALSE);
			}


			board[ dwJumpField ] = 0;
		}

		pMoveStep = pNextStep;
		pNextStep++;
		stepCount++;
	}

	pMove->numMoves = stepCount;
	
	// check the King conversion
	register BYTE dwKingField = *pMoveStep;
	if( CHANGE_TO_King( dwKingField ) == true && IS_MAN(board[dwKingField]) == true )
	{
		ChangeToKing( board, dwKingField, boardKey );
		pMove->Crowned = TRUE;
	}
}

void UndoMove(TGameBoard board, TSingleMove* pMove, bool bJump, ZOBRIST_KEY& boardKey)
{
	BYTE* pFirstStep = (BYTE*)pMove->MovePositions;
	BYTE* pLastStep = ((BYTE*)pMove->MovePositions) + pMove->numMoves;

	// check man conversion
	if( pMove->Crowned == TRUE)
	{
		register BYTE dwKingField = *pLastStep;
		ChangeToMan( board, dwKingField, boardKey );
	}
	int numSteps = pMove->numMoves;
	// undo the moves
	while(pLastStep != pFirstStep)
	{
		register BYTE dwCurField = *pLastStep;
		register BYTE dwPreviousField = *(pLastStep - 1);

		if(board[dwPreviousField] == BORDER)
		{
			assert( FALSE );
		}


		board[ dwPreviousField ] = board[ dwCurField ];
		board[ dwCurField ] = 0;



		if(bJump == true)
		{
			register BYTE dwJumpField = pMove->RemovePositions[numSteps-1];
			register BYTE dwJumpedFigure = pMove->RemovedFigures[numSteps-1];
			if(board[dwJumpField] == BORDER)
			{
				assert(FALSE);				
			}
			board[ dwJumpField ] = dwJumpedFigure;

		}

		numSteps--;
		pLastStep--;
	} // while
}
